<%@ page language="java" contentType="text/html; charset=utf-8" %>
<jsp:useBean id="rs" class="weaver.conn.RecordSet" scope="page"/>
<jsp:useBean id="bb" class="weaver.general.BaseBean"/>

<%@ page import="com.alibaba.fastjson.JSONArray" %>
<%@ page import="com.alibaba.fastjson.JSONObject" %>
<%@ page import="weaver.common.DateUtil" %>
<%@ page import="weaver.conn.RecordSet" %>
<%@ page import="weaver.general.Util" %>
<%
    JSONObject jsonData = new JSONObject();
    RecordSet rs1 = new RecordSet();
    try {
       String fullDate = DateUtil.getFullDate();
        bb.writeLog(">>>>>synHttzApi>>>>>fullDate="+fullDate);
        String pages = Util.null2String(request.getParameter("pages"));
        String pagenum = Util.null2String(request.getParameter("pagenum"));
        String id = Util.null2String(request.getParameter("id"));
        bb.writeLog(">>>>>synHttzApi>>>>>pages="+pages+",pagenum="+pagenum);
        JSONArray dataList = new JSONArray();
        String sql = "select * from uf_httz where id in(" +
                "'11-JJ-202305-0204','11-CG-202306-0141','11-SJ05-201901-0221-2','11-JJ-202306-0057'," +
                "'11-JJ-202306-0056','95-JJ-202306-0018','24-CG-202307-0166','24-CG-202307-0165') and ifnull(sftbcg,1)=1 ";
        String sqlwhere = "";
        String orderby = "";
        if(!"".equals(pages) && !"".equals(pages)){
            orderby +=" order by id limit "+pages+" offset "+pagenum;
        }
        if(!"".equals(id)){
            sqlwhere +=" and id='"+id+"'";
        }
        rs.executeQuery(sql+sqlwhere+orderby);
        int counts = rs.getCounts();
        int succcount = 0;
        int failcount = 0;
        while (rs.next()){
            JSONObject paramjson = new JSONObject();
            String mainid = Util.null2String(rs.getString("id"));
            String htbh = Util.null2String(rs.getString("htbh"));
            String ngr = Util.null2String(rs.getString("a4"));
            String ngrgh = getWorkCode(ngr);
            String htje = Util.null2String(rs.getString("htje"));
            String htmc = Util.null2String(rs.getString("htmc"));
            String bssj = Util.null2String(rs.getString("a5"));
            String htssgs = Util.null2String(rs.getString("htssgs"));
            String bm = Util.null2String(rs.getString("bm"));
            String kssj = Util.null2String(rs.getString("kssj"));
            String jssj = Util.null2String(rs.getString("jssj"));
            String htbdw = Util.null2String(rs.getString("htbdw"));
            String htqdmd = Util.null2String(rs.getString("htqdmd"));
            String bz = Util.null2String(rs.getString("bz"));
            String tjsj = Util.null2String(rs.getString("tjsj"));
            String gdsj = Util.null2String(rs.getString("gdsj"));
            JSONObject contractHead = new JSONObject();
            contractHead.put("contractNumber", mainid);
            contractHead.put("originContractNumber", htbh);
            contractHead.put("setOfBooksCode", "DEFAULT_SOB");
            contractHead.put("applicantEmployeeId", ngrgh);
            if (Util.getDoubleValue(htje) > 0) {
                contractHead.put("contractCategory", "CONTRACT");
                contractHead.put("contractTypeCode", "CON001");
            } else {
                contractHead.put("contractCategory", "FRAMEWORK_AGREEMENT");
                contractHead.put("contractTypeCode", "CON002");
            }
            String gscode = getGsfCode(htssgs);
            contractHead.put("companyCode", gscode);
            contractHead.put("contractName", htmc);
            contractHead.put("signDate", bssj);
            contractHead.put("partnerCategory", "VENDOR");
            String xdfcode = getXdfCode(mainid);
            contractHead.put("partnerCode", xdfcode);
            contractHead.put("amount", htje);
            contractHead.put("currencyCode", "CNY");
            contractHead.put("remark", InvokeHelper.removeHTMLTags(Util.null2String(bz)));
            contractHead.put("liableCustDeptNum", bm);
            contractHead.put("liablePersonEmployeeId", ngrgh);
            contractHead.put("startDate", kssj);
            contractHead.put("endDate", jssj);
            contractHead.put("status", null);
            JSONArray customFormValues = new JSONArray();
            JSONObject customField1 = new JSONObject();
            customField1.put("fieldCode", "BDW");
            customField1.put("value", htbdw);
            customFormValues.add(customField1);
            JSONObject customField2 = new JSONObject();
            customField2.put("fieldCode", "QDMD");
            customField2.put("value", htqdmd);
            customFormValues.add(customField2);
            contractHead.put("customFormValues", customFormValues);
            contractHead.put("createdDate", bssj);
            contractHead.put("submittedDate", tjsj);
            contractHead.put("lastApprovalDate",gdsj);
            paramjson.put("contractHead", contractHead);
            JSONArray contractLines = new JSONArray();
            rs1.executeQuery("select * from uf_httz_dt2 where mainid=?", mainid);
            while (rs1.next()) {
                JSONObject contractLine = new JSONObject();
                String fkjdbm = Util.null2String(rs1.getString("fkjdbm"));
                String hthje = Util.null2String(rs1.getString("je"));
                String yzfje = Util.null2String(rs1.getString("yzfje"));
                String fktj = Util.null2String(rs1.getString("fktj"));
                String yjfkrq = Util.null2String(rs1.getString("yjfkrq"));
                contractLine.put("stageCode", fkjdbm);
                contractLine.put("amount", hthje);
                contractLine.put("partnerCode", xdfcode);
                contractLine.put("dueDate", yjfkrq);
                contractLine.put("stagePaidPaymentAmount", yzfje);
                contractLine.put("stagePaidAdvancesAmount", 0);
                contractLine.put("partnerBankAccountNo", "");
                contractLine.put("paymentConditions", fktj);
                contractLines.add(contractLine);
            }
            paramjson.put("contractLines", contractLines);
            bb.writeLog(">>>>>synHttzApi>>>>>dataid="+mainid);
            bb.writeLog(">>>>>synHttzApi>>>>>paramjson="+paramjson);
            String resultStr = InvokeHelper.contract(paramjson.toJSONString());
            bb.writeLog(">>>>>synHttzApi>>>>>resultStr="+resultStr);
            JSONObject resultJson = JSONObject.parseObject(resultStr);
            String message = Util.null2String(resultJson.getString("message"));
            if ("operation completed".equals(message)) {
                succcount++;
                rs.executeUpdate("update uf_httz set sftbcg=0 where id=?",mainid);
            }else {
                failcount++;
                rs.executeUpdate("update uf_httz set sftbcg=1 where id=?",mainid);
                JSONObject dataJson = new JSONObject();
                dataJson.put("dataid",mainid);
                dataJson.put("result",resultStr);
                dataList.add(dataJson);
            }
        }
        jsonData.put( "data", dataList );
        jsonData.put( "result", "同步总记录数：["+counts+"]，成功记录数：["+succcount+"]，失败记录数["+failcount+"]" );
    }catch (Exception e){
        e.printStackTrace();
        out.print("同步代码运行异常");
    }

    out.print( jsonData);
%>
<%!

    public static String getXdfCode(String mainid){
        RecordSet rs = new RecordSet();
        rs.executeQuery("select dfdm from uf_httz_dt1 where mainid=?",mainid);
        rs.next();
        return Util.null2String(rs.getString("dfdm"));
    }

    public static String getGsfCode(String htssgs){
        RecordSet rs = new RecordSet();
        rs.executeQuery("select sapbm from uf_sapbmdzb where htszgs=?",htssgs);
        rs.next();
        return Util.null2String(rs.getString("sapbm"));
    }
    public static String getWorkCode(String resourceid){
        RecordSet rs = new RecordSet();
        rs.executeQuery("select workcode from hrmresource where id=?",resourceid);
        rs.next();
        return Util.null2String(rs.getString("workcode"));
    }
%>